<?php

	/**
	* @author zhouguoyan
	* @desc 新闻管理
	**/	
	
class CircularController extends AppController {
	var $name='Circular';
	var $uses=array('News');
	var $access = array ('news_index'=>array('role' => array('System')),
					     'news_detail'=>array('role' => array('System')),
					     'news_add'=>array('role' => array('System')),
					     'news_confirm'=>array('role' => array('System')),
					     'news_edit'=>array('role' => array('System')),
					     'news_doadd'=>array('role' => array('System')),
					     'dodelete'=>array('role' => array('System'))
					      );
	function index(){
		
	}
	/**
	* @author zhouguoyan
	* @desc 进入添加页面
	**/	
	function news_index(){
		$condition="\"News\".\"deleted\" is null";
		$data = $this->News->findAll($condition,null,"News.modified DESC",$this->show,$this->page);
		$this->setValue($data);
		$count = $this->News->findCount($condition);
		
		$this->setPaging('ajax','/circular/news_index',$count);
		
		$this->set('role',$this->rdAuth->role);
		$this->set('data',$data);
	}

	/**
	* @author zhouguoyan
	* @desc 进入详细页面 $ID是News.id
	**/			
	function news_detail($id){
		$condition="\"News\".\"deleted\" is null AND \"News\".\"id\"=" . $id;
		$this->data = $this->News->find($condition);
		
	}
	
	/**
	* @author zhouguoyan
	* @desc 新闻添加
	**/		
	function news_add(){
	
		$news_info=$this->Session->read('news_info');
	
		if(!empty($news_info)){
			$this->data=$news_info;
		}
	}
	
	/**
	* @author zhouguoyan
	* @desc 新闻验证
	**/	
	function news_confirm(){
		$message = $this->News->valid($this->Valid,$this->data);

		$this->Session->write('news_info',$this->data);

		if(!empty($message)){
			$this->set('message',$message);
			
			$this->render('news_add');
			exit;
		}

	}	
	
	/**
	* @author zhouguoyan
	* @desc  新闻编辑
	**/	
	function news_edit($id=null){
			$data=$this->News->find("\"News\".\"deleted\" is null AND \"News\".\"id\"=".$id);
			if(empty($data)){ 
				$this->news_index();	
			}else{
				$this->data=$data;
				
				$this->render('news_add');
			}
			
	}
	/**
	* @author zhouguoyan
	* @desc 把数据添加数据库
	**/	
	function news_doadd(){
		$id=$this->data['News']['id'];
		if( $id==null){
			if($this->Session->check('news_info')){
				$this->News->save($this->Session->read('news_info'));
			}
		}
		else{
				$this->News->save($this->Session->read('news_info'));
		}		
		$this->Session->del('news_info');
		
	}
	/**
	* @author zhouguoyan
	* @desc 删除数据
	**/	
	function dodelete(){
		$delete = array ();
		
		if (isset ($_COOKIE['new_check_delete']))
		{
			
			$delete = split(',', $_COOKIE['new_check_delete']);
			$str=implode(',',$delete);
			$this->News->doDelete($str);
			
			$this->redirect("/circular/news_index");
			exit;

		}
	}

	function setValue(& $data) {

		$delete = array ();

		if (isset ($_COOKIE['new_check_delete']))
			$delete = split(',', $_COOKIE['new_check_delete']);
		
		foreach ($data as & $v) {
			$id = $v['News']['id'];
			if (in_array($id, $delete)) {
				$v['News']['delete'] = $id;
			}
		}
	}
}
?>
